# -*- coding: utf-8 -*- 

'''
Created on 2013.6.29
登陆控制检查模块

@author: lzy
'''
import logging
from framework import bottle
from framework.bottle import Bottle,redirect,request
from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader
from framework.gaesessions import get_current_session

#jinja2
env = Environment(loader = FileSystemLoader('static/templates'))

bottle.debug(True)
loginController = Bottle()
       
# 首页登陆的验证请求 
@loginController.route('/',method='POST')
def loginm():
    loginStatus = loginCheck()
    
    if(loginStatus == True):
        return {'success': 'true'}
    else:
        return {'success': 'false'}

#单独登陆页渲染请求
@loginController.route('/login.html',method='GET')   
def get_loginw():
    template = env.get_template('login.html')
    return template.render()

# 单独登陆页验证请求
@loginController.route('/login.html',method='POST')   
def loginw():
    loginStatus = loginCheck()
    
    if(loginStatus == True):
        redirect("/record/index.html")
    else:
        redirect("/login/login.html")
   
#辅助函数-登陆检验 
def loginCheck():
    logging.info("===debug info===")
    username = request.forms.get('username')
    password = request.forms.get('password')
    
    logging.info(username + ":" + password)
    
    # 登陆成功放置session
    if(cmp(username, 'lzy') == 0 and cmp(password, 'lzy') == 0):
        logging.info("login success")
            
        session = get_current_session()
        session['username'] = username
        
        return True
    else:
        logging.info("login failed")
        return False

#辅助函数-跳转页面
def redirect_msg(url, msg):       
    template = env.get_template('record.html')
    return template.render(url, msg=msg)
    
    

    
        
    